{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random \n",
    "\n",
    "src = [chr(i) for i in range(33, 127)]\n",
    "tar = src.copy()\n",
    "random.shuffle(tar)\n",
    "s2t = dict()\n",
    "t2s = dict()\n",
    "\n",
    "for i in range(len(src)):\n",
    "    s2t[src[i]] = tar[i]\n",
    "    t2s[tar[i]] = src[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def convert_char(s: str, op: str) -> str:\n",
    "    '''对单个字符进行加密\n",
    "    输入参数：\n",
    "    single_char: 要加密的单个字符\n",
    "    operation: 加密还是解密，encrypt->加密，decrypt->解密\n",
    "    返回结果：加密/解密后的单个字符\n",
    "    '''\n",
    "    global src, tar\n",
    "    res = ''\n",
    "    if ord(s) >= 33 and ord(s) <= 126:\n",
    "        if op == 'encrypt':\n",
    "            res = s2t[s]\n",
    "        elif op == 'decrypt':\n",
    "            res = t2s[s]\n",
    "    else:\n",
    "        res = s\n",
    "    return res\n",
    "\n",
    "def encrypt_it(s: str) -> str:\n",
    "    '''用于对字符串进行简单替换加密\n",
    "    输入参数：\n",
    "    src_str： 原始文本内容\n",
    "    返回结果：加密/解密文本\n",
    "    '''\n",
    "    res = ''\n",
    "    for ch in s:\n",
    "        res += convert_char(ch, 'encrypt')\n",
    "    return res\n",
    "\n",
    "def decrypt_it(s: str) -> str:\n",
    "    '''用于对字符串进行简单替换解密\n",
    "    输入参数：\n",
    "    encrypted_str： 加密文本内容\n",
    "    返回结果：解密文本\n",
    "    '''\n",
    "    res = ''\n",
    "    for ch in s:\n",
    "        res += convert_char(ch, 'decrypt')\n",
    "    return res\n",
    "\n",
    "assert(decrypt_it(encrypt_it('AbCdefgH!')) == 'AbCdefgH!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = {\n",
    "    (1, 2, 3): (2, 3, 4)\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "(2, 3, 4)"
     },
     "metadata": {},
     "execution_count": 11
    }
   ],
   "source": [
    "a[(1, 2, 3)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "{}"
     },
     "metadata": {},
     "execution_count": 13
    }
   ],
   "source": [
    "a = dict()\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "{3: {1, 2, 3}}"
     },
     "metadata": {},
     "execution_count": 15
    }
   ],
   "source": [
    "a[3] = {1, 2, 3}\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "3\n{1, 2, 3}\n"
    }
   ],
   "source": [
    "for i in a:\n",
    "    print(i)\n",
    "    print(a[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "dict_keys([3])"
     },
     "metadata": {},
     "execution_count": 21
    }
   ],
   "source": [
    "a.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "dict_values([{1, 2, 3}])"
     },
     "metadata": {},
     "execution_count": 22
    }
   ],
   "source": [
    "a.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "dict_items([(3, {1, 2, 3})])"
     },
     "metadata": {},
     "execution_count": 23
    }
   ],
   "source": [
    "a.items()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "3\n"
    }
   ],
   "source": [
    "for k in a.keys():\n",
    "    print(k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "3 {1, 2, 3}\n"
    }
   ],
   "source": [
    "for k, v in a.items():\n",
    "    print(k, v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "class CryptIt:\n",
    "    '''\n",
    "    '''\n",
    "    def __init__(self):\n",
    "        self.src = [chr(i) for i in range(33, 127)]\n",
    "        self.tar = self.src.copy()\n",
    "        self.s2t = dict()\n",
    "        self.t2s = dict()\n",
    "        random.shuffle(self.tar)\n",
    "        for i in range(len(self.src)):\n",
    "            self.s2t[self.src[i]] = self.tar[i]\n",
    "            self.t2s[self.tar[i]] = self.src[i]\n",
    "\n",
    "    def convert_char(self, s: str, op: str) -> str:\n",
    "        '''对单个字符进行加密\n",
    "        输入参数：\n",
    "        single_char: 要加密的单个字符\n",
    "        operation: 加密还是解密，encrypt->加密，decrypt->解密\n",
    "        返回结果：加密/解密后的单个字符\n",
    "        '''\n",
    "        res = ''\n",
    "        if ord(s) >= 33 and ord(s) <= 126:\n",
    "            if op == 'encrypt':\n",
    "                res = self.s2t[s]\n",
    "            elif op == 'decrypt':\n",
    "                res = self.t2s[s]\n",
    "        else:\n",
    "            res = s\n",
    "        return res\n",
    "\n",
    "    def encrypt_it(self, s: str) -> str:\n",
    "        '''用于对字符串进行简单替换加密\n",
    "        输入参数：\n",
    "        src_str： 原始文本内容\n",
    "        返回结果：加密/解密文本\n",
    "        '''\n",
    "        res = ''\n",
    "        for ch in s:\n",
    "            res += self.convert_char(ch, 'encrypt')\n",
    "        return res\n",
    "\n",
    "    def decrypt_it(self, s: str) -> str:\n",
    "        '''用于对字符串进行简单替换解密\n",
    "        输入参数：\n",
    "        encrypted_str： 加密文本内容\n",
    "        返回结果：解密文本\n",
    "        '''\n",
    "        res = ''\n",
    "        for ch in s:\n",
    "            res += self.convert_char(ch, 'decrypt')\n",
    "        return res\n",
    "    \n",
    "    def assert_crypt(self):\n",
    "        assert(self.decrypt_it(self.encrypt_it('AbCdefgH!')) == 'AbCdefgH!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "<__main__.CryptIt at 0x261ab2cc948>"
     },
     "metadata": {},
     "execution_count": 2
    }
   ],
   "source": [
    "my_crypt = CryptIt()\n",
    "my_crypt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "'U.!@&yZnL'"
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "source": [
    "my_crypt.encrypt_it('AbCdefgH!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "'AbCdefgH!'"
     },
     "metadata": {},
     "execution_count": 5
    }
   ],
   "source": [
    "my_crypt.decrypt_it('U.!@&yZnL')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "'26F9%7<li'"
     },
     "metadata": {},
     "execution_count": 6
    }
   ],
   "source": [
    "my_crypt.encrypt_it('AbCdefgH!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "'AbCdefgH!'"
     },
     "metadata": {},
     "execution_count": 7
    }
   ],
   "source": [
    "my_crypt.decrypt_it('U.!@&yZnL')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_crypt.assert_crypt()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "src_text = \"\"\"\n",
    "A\t·-\n",
    "H\t····\n",
    "O\t---\n",
    "V\t···-\n",
    "B\t-···\n",
    "I\t··\n",
    "P\t·--·\n",
    "W\t·--\n",
    "C\t-·-·\n",
    "J\t·---\n",
    "Q\t--·-\n",
    "X\t-··-\n",
    "D\t-··\n",
    "K\t-·-\n",
    "R\t·-·\n",
    "Y\t-·--\n",
    "D\t-··\n",
    "K\t-·-\n",
    "R\t·-·\n",
    "Y\t-·--\n",
    "E\t·\n",
    "L\t·-··\n",
    "S\t···\n",
    "Z\t--··\n",
    "F\t··-·\n",
    "M\t--\n",
    "T\t-\n",
    "G\t--·\n",
    "N\t-·\n",
    "U\t··-\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "ms = dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "30.0"
     },
     "metadata": {},
     "execution_count": 14
    }
   ],
   "source": [
    "items = src_text.split()\n",
    "len(items) / 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "{'A': '·-',\n 'H': '····',\n 'O': '---',\n 'V': '···-',\n 'B': '-···',\n 'I': '··',\n 'P': '·--·',\n 'W': '·--',\n 'C': '-·-·',\n 'J': '·---',\n 'Q': '--·-',\n 'X': '-··-',\n 'D': '-··',\n 'K': '-·-',\n 'R': '·-·',\n 'Y': '-·--',\n 'E': '·',\n 'L': '·-··',\n 'S': '···',\n 'Z': '--··',\n 'F': '··-·',\n 'M': '--',\n 'T': '-',\n 'G': '--·',\n 'N': '-·',\n 'U': '··-'}"
     },
     "metadata": {},
     "execution_count": 17
    }
   ],
   "source": [
    "for i in range(int(len(items) / 2)):\n",
    "    ms[items[2 * i]] = items[2 * i + 1]\n",
    "ms"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "['A',\n '·-',\n 'H',\n '····',\n 'O',\n '---',\n 'V',\n '···-',\n 'B',\n '-···',\n 'I',\n '··',\n 'P',\n '·--·',\n 'W',\n '·--',\n 'C',\n '-·-·',\n 'J',\n '·---',\n 'Q',\n '--·-',\n 'X',\n '-··-',\n 'D',\n '-··',\n 'K',\n '-·-',\n 'R',\n '·-·',\n 'Y',\n '-·--',\n 'D',\n '-··',\n 'K',\n '-·-',\n 'R',\n '·-·',\n 'Y',\n '-·--',\n 'E',\n '·',\n 'L',\n '·-··',\n 'S',\n '···',\n 'Z',\n '--··',\n 'F',\n '··-·',\n 'M',\n '--',\n 'T',\n '-',\n 'G',\n '--·',\n 'N',\n '-·',\n 'U',\n '··-']"
     },
     "metadata": {},
     "execution_count": 20
    }
   ],
   "source": [
    "m = dir()\n",
    "src_text.split()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "Help on built-in function split:\n\nsplit(sep=None, maxsplit=-1) method of builtins.str instance\n    Return a list of the words in the string, using sep as the delimiter string.\n    \n    sep\n      The delimiter according which to split the string.\n      None (the default value) means split according to any whitespace,\n      and discard empty strings from the result.\n    maxsplit\n      Maximum number of splits to do.\n      -1 (the default value) means no limit.\n\n"
    }
   ],
   "source": [
    "help(src_text.split)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "src_text = \"\"\"A\t·-\n",
    "H\t····\n",
    "O\t---\n",
    "V\t···-\n",
    "B\t-···\n",
    "I\t··\n",
    "P\t·--·\n",
    "W\t·--\n",
    "C\t-·-·\n",
    "J\t·---\n",
    "Q\t--·-\n",
    "X\t-··-\n",
    "D\t-··\n",
    "K\t-·-\n",
    "R\t·-·\n",
    "Y\t-·--\n",
    "D\t-··\n",
    "K\t-·-\n",
    "R\t·-·\n",
    "Y\t-·--\n",
    "E\t·\n",
    "L\t·-··\n",
    "S\t···\n",
    "Z\t--··\n",
    "F\t··-·\n",
    "M\t--\n",
    "T\t-\n",
    "G\t--·\n",
    "N\t-·\n",
    "U\t··-\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "['A',\n '·-',\n 'H',\n '····',\n 'O',\n '---',\n 'V',\n '···-',\n 'B',\n '-···',\n 'I',\n '··',\n 'P',\n '·--·',\n 'W',\n '·--',\n 'C',\n '-·-·',\n 'J',\n '·---',\n 'Q',\n '--·-',\n 'X',\n '-··-',\n 'D',\n '-··',\n 'K',\n '-·-',\n 'R',\n '·-·',\n 'Y',\n '-·--',\n 'D',\n '-··',\n 'K',\n '-·-',\n 'R',\n '·-·',\n 'Y',\n '-·--',\n 'E',\n '·',\n 'L',\n '·-··',\n 'S',\n '···',\n 'Z',\n '--··',\n 'F',\n '··-·',\n 'M',\n '--',\n 'T',\n '-',\n 'G',\n '--·',\n 'N',\n '-·',\n 'U',\n '··-']"
     },
     "metadata": {},
     "execution_count": 30
    }
   ],
   "source": [
    "src_text.split()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "m = dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "{'A': '·-',\n 'H': '····',\n 'O': '---',\n 'V': '···-',\n 'B': '-···',\n 'I': '··',\n 'P': '·--·',\n 'W': '·--',\n 'C': '-·-·',\n 'J': '·---',\n 'Q': '--·-',\n 'X': '-··-',\n 'D': '-··',\n 'K': '-·-',\n 'R': '·-·',\n 'Y': '-·--',\n 'E': '·',\n 'L': '·-··',\n 'S': '···',\n 'Z': '--··',\n 'F': '··-·',\n 'M': '--',\n 'T': '-',\n 'G': '--·',\n 'N': '-·',\n 'U': '··-'}"
     },
     "metadata": {},
     "execution_count": 34
    }
   ],
   "source": [
    "for line in src_text.split('\\n'):\n",
    "    k, v = line.split('\\t')\n",
    "    m[k] = v\n",
    "m"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}